package com.heer.gymnasium.activity;

import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.heer.api.ResponseData.ApiResponse;
import com.heer.api.callback.BaseCallback;
import com.heer.gymnasium.R;
import com.heer.gymnasium.adapter.MyWalletAdapter;
import com.heer.gymnasium.util.Session;
import com.heer.model.MyWalletBean;

import java.util.List;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by heer on 17/4/9.
 */

public class MyWalletActivity extends BaseRefreshActivity {
    @BindView(R.id.list)
    ListView list;

    private MyWalletAdapter mAdapter;

    @Override
    protected void init() {
        super.init();
        setContentView(R.layout.activity_my_wallet);
    }

    @Override
    protected void initView() {
        super.initView();
        list.setAdapter(mAdapter = new MyWalletAdapter(mContext));
        titleBuilder.setTitleText("我的钱包");
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                RechargeOfferActivity.toRechareOffer(mContext, mAdapter.getItem(position).getId());
            }
        });
    }

    @Override
    protected void loadData(final boolean loadMore) {
        super.loadData(loadMore);
        mAppHelper.apiService().getMyWallet(Session.getUserId()).enqueue(new BaseCallback<ApiResponse<List<MyWalletBean>>>() {
            @Override
            public void onResponse(Call<ApiResponse<List<MyWalletBean>>> call, Response<ApiResponse<List<MyWalletBean>>> response) {
                super.onResponse(call, response);
                layoutRefresh.setRefreshing(false);
                if (response.isSuccessful() && response.body().isSccessful())
                    if (loadMore)
                        mAdapter.addItems(response.body().getData());
                    else
                        mAdapter.setItems(response.body().getData());
                if (response.body().isTokenExpire()) {
                    toLogin();
                }
            }

            @Override
            public void onFailure(Call<ApiResponse<List<MyWalletBean>>> call, Throwable t) {
                super.onFailure(call, t);
                layoutRefresh.setRefreshing(false);
            }
        });
    }

}
